package com.example.smartf.controller;

import com.example.smartf.model.mysql.Automaticplan;
import com.example.smartf.service.AutomaticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("自动化控制")
@CrossOrigin(origins = "*")
@RestController
public class AutomaticController {

    @Autowired
    private AutomaticService automaticService;

    @ApiOperation("显示所有自动化控制方案")
    @ApiImplicitParam(name = "deviceID" , value = "设备ID" , dataType = "String" , paramType = "query")
    @PostMapping(value = "/automatic/showall")
    List<Automaticplan> showAll(@RequestParam("deviceID") String deviceID){
        return automaticService.showall(deviceID);
    }


    //添加一个自动控制方案
    @ApiOperation("添加一个自动控制方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "plan", value = "方案", dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "deviceID", value = "设备ID", dataType = "String" , paramType = "query")
    })

    @PostMapping(value = "/automatic/add")
    String updateTrainDataCgr(@RequestParam("plan") String plan,
                              @RequestParam("deviceID") String deviceID){
        JSONObject jsonObject = JSONObject.fromObject(plan);
        int hour_c,min_c,ec,light,pump_time,fan_time;
        try {
            hour_c = jsonObject.getInt("hour_c");
            min_c = jsonObject.getInt("min_c");
        }catch (JSONException e){
            return "error";
        }
        try {
            ec = jsonObject.getInt("ec");
        }catch (JSONException e){
            ec = 0;
        }

        //控制 EC 值 符合实际
        if( ec > 3000 || ec < 500){
            return "error";
        }

        try {
            light = jsonObject.getInt("light");
        }catch (JSONException e){
            light = 0;
        }
        try {
            pump_time = jsonObject.getInt("pump_time");
        }catch (JSONException e){
            pump_time = 0;
        }
        try {
            fan_time = jsonObject.getInt("fan_time");
        }catch (JSONException e){
            fan_time = 0;
        }

        return automaticService.addPlan(deviceID,hour_c,min_c,ec,light,pump_time,fan_time);
    }

    @ApiOperation("删除一个方案（方案需要不在使用中）")
    @ApiImplicitParam(name = "planID" , value = "方案ID" , dataType = "String" , paramType = "query")
    @PostMapping(value = "/automatic/delplan")
    String delPlan(@RequestParam("planID") String planID){
        try {
            return automaticService.delPlan(Integer.parseInt(planID));
        }catch (Exception e){
            return "failed";
        }

    }

}
